package com.ruoyi.framework.aspectj;

import com.ruoyi.common.annotation.BasicFieldFilling;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 数据基础字段填充
 *
 * @author ruoyi
 */
@Aspect
@Component
public class BasicFieldFillingAspect
{

    @Before("@annotation(controllerBasicFieldFilling)")
    public void doBefore(JoinPoint point, BasicFieldFilling controllerBasicFieldFilling) throws Throwable
    {
        handleDataScope(point, controllerBasicFieldFilling);
    }

    protected void handleDataScope(final JoinPoint joinPoint, BasicFieldFilling controllerBasicFieldFilling)
    {
        // 获取当前的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNotNull(loginUser))
        {
            SysUser currentUser = loginUser.getUser();
            // 如果是超级管理员，则不过滤数据
            if (StringUtils.isNotNull(currentUser))
            {
                if("dynamic".equals(controllerBasicFieldFilling.filterType())){
                    if("add".equals(controllerBasicFieldFilling.operationType())){
                        dynamicDataScopeAdd(joinPoint, currentUser);
                    }else if("edit".equals(controllerBasicFieldFilling.operationType())){
                        dynamicDataScopeEdit(joinPoint, currentUser);
                    }
                }

            }
        }
    }

    public static void dynamicDataScopeAdd(JoinPoint joinPoint, SysUser user){
        Object params = joinPoint.getArgs()[0];
        if (StringUtils.isNotNull(params) && params instanceof Map) {
            HashMap<String, Object> paramsMap = (HashMap<String, Object>) params;
            paramsMap.put("create_time", DateUtils.getTime());
            paramsMap.put("update_time", DateUtils.getTime());
            paramsMap.put("create_user_name", user.getNickName());
            paramsMap.put("create_user_id", user.getUserId());
            paramsMap.put("create_dept_name", user.getDept().getDeptName());
            paramsMap.put("create_dept_id", user.getDeptId());
            paramsMap.put("status", "0");
        }
    }

    public static void dynamicDataScopeEdit(JoinPoint joinPoint, SysUser user){
        Object params = joinPoint.getArgs()[0];
        if (StringUtils.isNotNull(params) && params instanceof Map) {
            HashMap<String, Object> paramsMap = (HashMap<String, Object>) params;
            paramsMap.put("update_time", DateUtils.getTime());
            paramsMap.put("update_user_name", user.getNickName());
            paramsMap.put("update_user_id", user.getUserId());
            paramsMap.put("status", "0");
        }
    }

}
